<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>数据库管理</title>
<link href="css/default.css" rel="stylesheet" type="text/css" />
<script src="js/page.js"></script>
<jsp:useBean id="database" class="DatabaseConnection.DatabaseConnection" />
<%@include file="connect_database.jsp" %>
<%
request.setCharacterEncoding("utf-8");
String database_name = request.getParameter("database");
String table_name = request.getParameter("table");
String sql_columns = "show columns from " + table_name + ";";
ResultSet resultset_columns = database.executeQuery(sql_columns, database_name);
String [] columns = null;
String [] extras = null;
int first_editable_column_index = -1;
if (resultset_columns != null)
{
	String columns_string = "";
	String extras_string = "";
	int index = 0;
	while (resultset_columns.next() == true)
	{
		columns_string = columns_string + resultset_columns.getString("Field") + ",";
		extras_string = extras_string + (resultset_columns.getString("Extra").length() > 0 ? resultset_columns.getString("Extra") : "#") + ",";
		if (first_editable_column_index == -1 && !resultset_columns.getString("Extra").equals("auto_increment"))
			first_editable_column_index = index;
		index++;
	}
	columns = columns_string.split(",");
	extras = extras_string.split(",");
}
if (first_editable_column_index == -1)
	first_editable_column_index = 0;
%>
<script type="text/javascript">
<!--
function mouseOverHandler(index)
{
	if (document.getElementById("choose_" + index).checked == true)
		document.getElementById("tr_" + index).style.backgroundColor = "#FFFFC1";
	else
		document.getElementById("tr_" + index).style.backgroundColor = "#FFFFE1";
}

function mouseOutHandler(index)
{
	if (document.getElementById("choose_" + index).checked == true)
		document.getElementById("tr_" + index).style.backgroundColor = "#F0F0F0";
	else
		document.getElementById("tr_" + index).style.backgroundColor = "#FFFFFF";
}

function clickHandler(index)
{
	document.getElementById("choose_" + index).checked = !document.getElementById("choose_" + index).checked;
	if (document.getElementById("choose_" + index).checked == true)
		document.getElementById("tr_" + index).style.backgroundColor = "#FFFFC1";
	else
		document.getElementById("tr_" + index).style.backgroundColor = "#FFFFE1";
	var choose_all = true;
	for (var i = 0; i < document.getElementsByName("choose").length; i++)
	{
		if (document.getElementsByName("choose").item(i).checked == false)
		{
			choose_all = false;
			break;
		}
	}
	if (choose_all == true)
		document.getElementById("choose_all").checked = true;
	else
		document.getElementById("choose_all").checked = false;
}

function chooseAllHandler()
{
	var choose_all = document.getElementById("choose_all").checked;
	var choose = document.getElementsByName("choose");
	for (var i = 0; i < choose.length; i++)
	{
		choose.item(i).checked = choose_all;
		if (choose_all == true)
			document.getElementById("tr_" + choose.item(i).id.substring("choose_".length)).style.backgroundColor = "#F0F0F0";
		else
			document.getElementById("tr_" + choose.item(i).id.substring("choose_".length)).style.backgroundColor = "#FFFFFF";
	}
}

function insertClickHandler()
{
	var index = parseInt(document.getElementsByName("choose").item(document.getElementsByName("choose").length - 1).id.substring("choose_".length)) + 1;
	var new_row = document.getElementById("table_record").insertRow(document.getElementById("table_record").rows.length - 1);
	new_row.align = "center";
	new_row.bgColor = "#FFFFFF";
	new_row.id = "tr_" + index;
	var new_cell_0 = new_row.insertCell(0);
	new_cell_0.height = 30;
	new_cell_0.onmouseover = function(){mouseOverHandler(index);};
	new_cell_0.onmouseout = function(){mouseOutHandler(index);};
	new_cell_0.onclick = function(){clickHandler(index);};
	new_cell_0.innerHTML = "<input name='choose' type='checkbox' id='choose_" + index + "' onclick='clickHandler(" + index + ");' />";
	<%
	for (int i = 0; i < columns.length; i++)
	{
	%>
	var new_cell_<%= i + 1 %> = new_row.insertCell(<%= i + 1 %>);
	new_cell_<%= i + 1 %>.height = 30;
	new_cell_<%= i + 1 %>.innerHTML = "<textarea name='column_<%= columns[i] %>' id='column_<%= columns[i] %>_" + index + "' <%= extras[i].equals("auto_increment") ? "disabled='disabled'" : "" %> ><%= extras[i].equals("auto_increment") ? "（自动增长）" : "" %></textarea>";
	<%
	}
	%>
	document.getElementById("choose_all").checked = false;
	document.getElementById("column_<%= columns[first_editable_column_index] %>_" + index).focus();
}

function deleteClickHandler()
{
	var deleted = false;
	if (document.getElementById("choose_all").checked == true)
	{
		document.getElementsByName("choose").item(0).checked = false;
		document.getElementById("choose_all").checked = false;
		<%
		for (int i = 0; i < columns.length; i++)
		{
		%>
		document.getElementsByName("column_<%= columns[i] %>").item(0).value = "<%= extras[i].equals("auto_increment") ? "（自动增长）" : "" %>";
		<%
		}
		%>
		deleted = true;
		document.getElementsByName('column_<%= columns[first_editable_column_index] %>').item(0).focus();
	}
	for (var i = 0; i < document.getElementsByName("choose").length; i++)
	{
		if (document.getElementsByName("choose").item(i).checked == true)
		{
			document.getElementById("table_record").deleteRow(i + 1);
			deleted = true;
			i--;
		}
	}
	if (deleted == false)
		window.alert("您没有选择要删除的记录！");
	chooseAllHandler();
}

function okClickHandler()
{
	<%
	for (int i = 0; i < columns.length; i++)
	{
	%>
	var <%= columns[i] %>_data = "";
	for (var i = 0; i < document.getElementsByName("column_<%= columns[i] %>").length; i++)
	{
		<%= columns[i] %>_data = <%= columns[i] %>_data + (document.getElementsByName("column_<%= columns[i] %>").item(i).value == "" ? "#NULL#" : document.getElementsByName("column_<%= columns[i] %>").item(i).value) + "#,#";
	}
	document.getElementById("column_<%= columns[i] %>_data").value = <%= columns[i] %>_data;
	<%
	}
	%>
	document.getElementById("form_insert").submit();
}
//-->
</script>
</head>

<body onload="initialize();document.getElementById('column_<%= columns[first_editable_column_index] %>_1').focus();">
<%
String sql_check_table = "show tables like '" + table_name + "';";
ResultSet resultset_check_table = database.executeQuery(sql_check_table, database_name);
if (resultset_check_table != null && resultset_check_table.next() == true)
{
%>
<div style="padding-top:20px" />
<div align="center"><font size="+1"><strong>向数据表 <%= table_name %> 中添加记录</strong></font></div>
<br />
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#B1C8D7" id="table_record">
  <tr align="center" bgcolor="#F4F4FF">
    <td width="8%" height="24"><strong>选择</strong></td>
    <%
    for (int i = 0; i < columns.length; i++)
    {
    %>
    <td height="24"><strong><%= columns[i] %></strong></td>
    <%
    }
    %>
  </tr>
  <tr align="center" bgcolor="#FFFFFF" id="tr_1">
    <td height="30" onmouseover="mouseOverHandler(1);" onmouseout="mouseOutHandler(1);" onclick="clickHandler(1);"><input name="choose" type="checkbox" id="choose_1" onclick="clickHandler(1);" /></td>
    <%
    for (int i = 0; i < columns.length; i++)
    {
    %>
    <td height="30"><textarea name="column_<%= columns[i] %>" id="column_<%= columns[i] %>_1" <%= extras[i].equals("auto_increment") ? "disabled='disabled'" : "" %> ><%= extras[i].equals("auto_increment") ? "（自动增长）" : "" %></textarea></td>
    <%
    }
    %>
  </tr>
  <tr align="center" bgcolor="#F4F4FF">
    <td height="24">
      <input name="choose_all" type="checkbox" id="choose_all" onclick="chooseAllHandler();" />
      <label for="choose_all"><strong>全选</strong></label>
    </td>
    <td height="24" colspan="<%= columns.length %>">
      <input name="insert" type="button" class="input_button" id="insert" value="继续添加" onclick="insertClickHandler();" />
      　
      <input name="delete" type="button" class="input_button" id="delete" value="删除记录" onclick="deleteClickHandler();" />
      　
      <input name="ok" type="button" class="input_button" id="ok" value="确定" onclick="okClickHandler();" />
      　
      <input name="back" type="button" class="input_button" id="back" value="返回" onclick="window.location.href='records.jsp?database=<%= database_name %>&table=<%= table_name %>';" />
    </td>
  </tr>
</table>
<form id="form_insert" name="form_insert" method="post" action="records_insert_do.jsp?database=<%= database_name %>&table=<%= table_name %>">
  <%
  for (int i = 0; i < columns.length; i++)
  {
  %>
  <input name="column_<%= columns[i] %>_data" type="hidden" id="column_<%= columns[i] %>_data" />
  <%
  }
  %>
</form>
<%
}
else
{
%>
<script type="text/javascript">
<!--
	window.alert("打开数据表 <%= table_name %> 失败！");
	window.location.href = "tables.jsp?database=<%= database_name %>";
//-->
</script>
<%
}
if (resultset_columns != null)
	resultset_columns.close();
if (resultset_check_table != null)
	resultset_check_table.close();
database.closeConnection();
%>
</body>
</html>